package com.example.talkheart

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.method.HideReturnsTransformationMethod
import android.text.method.PasswordTransformationMethod
import android.view.View
import android.widget.*
import com.bumptech.glide.Glide
import com.example.talkheart.database.ScoreDatabase
import com.example.talkheart.utils.showToast


class LoginActivity : AppCompatActivity() {
    private val editTextPersonName: EditText by lazy { findViewById<EditText>(R.id.editTextPersonName) }
    private val editTextPassword: EditText by lazy { findViewById<EditText>(R.id.editTextPassword) }
    private val loginButton: Button by lazy { findViewById<Button>(R.id.loginButton) }
    private val savePasswordCheckBox: CheckBox by lazy { findViewById<CheckBox>(R.id.savePasswordCheckBox) }
    private val registerButton: Button by lazy { findViewById<Button>(R.id.registerButton) }
    private val passwordVisible: ImageView by lazy { findViewById<ImageView>(R.id.passwordVisible) }
    private val imageView5: ImageView by lazy { findViewById<ImageView>(R.id.imageView5) }
    private val teacherLoginCheckBox: CheckBox by lazy { findViewById<CheckBox>(R.id.teacherLoginCheckBox) }


    private val loginText : String = "登录"
    private val registerText : String = "注册"

    private var imageVisible : Boolean = true



    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.login_activity)

        loginButton.setOnClickListener{
            if(loginButton.text.equals(loginText)){
                val intent: Intent
                if(!teacherLoginCheckBox.isChecked)
                    intent = Intent(this,MainPageActivity::class.java)
                else
                    intent = Intent(this,TeacherActivity::class.java)
                "登录成功".showToast()

                startActivity(intent)
                //登录成功后关闭此活动
                finish()
            }else if(loginButton.text.equals(registerText)){
                "注册成功".showToast()
                setItemVisible(View.VISIBLE)
            }
            //进入主页面
        }
        registerButton.setOnClickListener {
            register()
        }
        passwordVisible.setOnClickListener{
            if(imageVisible){
                imageVisible = false
                passwordVisible.setImageResource(R.drawable.cant_see)
                editTextPassword.transformationMethod = PasswordTransformationMethod.getInstance()
            }else{
                imageVisible = true
                passwordVisible.setImageResource(R.drawable.see)
                editTextPassword.transformationMethod = HideReturnsTransformationMethod.getInstance()
            }
        }

        Glide.with(this)
            .load(R.drawable.login_log)
            .into(imageView5)
    }
    //登录注册都差不多，用一个XML就好了..
    private fun register(){
        setItemVisible(View.INVISIBLE)
    }

    private fun setItemVisible(visible: Int){
        savePasswordCheckBox.visibility = visible
        registerButton.visibility = visible
        if(visible.equals(View.VISIBLE)){
            loginButton.text = loginText
        }else if(visible.equals(View.INVISIBLE)){
            loginButton.text = registerText
        }
        clearText()
    }
    //登录注册时输入框里的信息也要清空！
    private fun clearText(){
        editTextPersonName.setText("")
        editTextPassword.setText("")
    }
}
